import _thread
import machine
import json
import app_config
import time

def wait_input_cmd(id):
    while True:
        try:
            inputcmd = input('please input cmd:')
            inputcmd_dict = json.loads(inputcmd)
            if isinstance(inputcmd_dict,dict):
                if 'triad' in inputcmd_dict and isinstance(inputcmd_dict['triad'],dict):
                    if "clientId" and "AccessToken"  and "ProjectKey" in inputcmd_dict["triad"] :
                        app_config.write_cfg((inputcmd_dict['triad']))
                        time.sleep_ms(200)
                        machine.reset()
                    else:
                        print("some is None:%s"%inputcmd_dict)

                if 'timestamp' in inputcmd_dict:
                    app_config.write_cfg({"timestamp":inputcmd_dict["timestamp"]})
                    time.sleep_ms(200)
                    machine.reset()

                if 'hearttime' in inputcmd_dict:
                    app_config.write_cfg({"hearttime":inputcmd_dict["hearttime"]})
                    time.sleep_ms(200)
                    machine.reset()

                if 'name' in inputcmd_dict:
                    app_config.write_cfg({"name":inputcmd_dict["name"]})
                    time.sleep_ms(200)
                    machine.reset()


            else:
                print("imput cmd is not a dict")
            
            pass
        except ValueError:
            '''other cmd process'''
            print('input cmd is:' + inputcmd)
            pass
        except KeyboardInterrupt:
            _thread.exit()

        finally:
            pass

def uart_thread():
    _thread.start_new_thread(wait_input_cmd,(4,))